package practice1_100;

public class Practice124 {
    int result=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        maxPathSum1(root);
        return result;
    }

    public int maxPathSum1(TreeNode root) {
        int sum=0;

//        return 8;
        if (root==null) return 0;

        int leftVal=maxPathSum1(root.left);
        int rightVal=maxPathSum1(root.right);

//        sum=1;
        sum+=leftVal>rightVal?leftVal:rightVal;
        if(sum+root.val<0) sum=0;
        else sum+=root.val;

        result=result>sum?result:sum;
        return sum;
    }

    public static void main(String[] args) {
        TreeNode root=new TreeNode(1,new TreeNode(2),new TreeNode(3));
        Practice124 practice124=new Practice124();
        int a=practice124.maxPathSum(root);
        System.out.println(a);
        System.out.println(practice124.result);
    }
}
